package com.example.demo.others.git;

import com.offbytwo.jenkins.JenkinsServer;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.time.Instant;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017-6-7.
 */
public class GitUtil {


    static Logger logger = LoggerFactory.getLogger(GitUtil.class);


    /**
     * @param git
     * @param pushMessage
     * @param tag
     * @param tagmsg
     * @throws IOException
     * @throws GitAPIException
     */
    public static void commit(final Git git, String pushMessage, String tag, String tagmsg) throws IOException, GitAPIException {
        git.diff().call().forEach(d -> {
            String s = d.getNewPath();
            System.out.println(s);
            try {
                git.add().addFilepattern(s).call();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        if(StringUtils.isBlank(pushMessage))
            pushMessage = DateFormatUtils.format(Instant.now().toEpochMilli(),"yyyyMMdd HH:mm");
        git.commit().setMessage(pushMessage).call();
        if (StringUtils.isNotBlank(tag) && StringUtils.isNotBlank(tagmsg)) {
            git.tag().setName(tag).setMessage(tagmsg).call();
        }
    }

    /**
     * @param name
     * @param git
     * @throws IOException
     * @throws GitAPIException
     */
    public static void push(String name, final Git git) throws IOException, GitAPIException {
        //推送到远程
        git.push().setPushAll().setCredentialsProvider(cre(name)).call().forEach(c -> {
            logger.info(c.getMessages());
        });
        git.push().setPushTags().setCredentialsProvider(cre(name)).call().forEach(c -> {
            logger.info(c.getMessages());
        });

    }

    /**
     * @param name
     * @param localRepoGitConfig
     * @throws IOException
     * @throws GitAPIException
     */
    public static void pull(String name, String localRepoGitConfig) throws IOException, GitAPIException {
        final Git git = open(localRepoGitConfig);
        try {

            String s = git.pull().setCredentialsProvider(cre(name)).call().getMergeResult().toString();

            logger.info(s);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            git.close();
        }
    }

    /**
     * @param localRepoGitConfig
     * @return
     * @throws IOException
     * @throws GitAPIException
     */
    public static Git open(String localRepoGitConfig) throws IOException, GitAPIException {
        final Git git = Git.open(new File(localRepoGitConfig));
        return git;
    }

    public static void close(Git git) throws IOException, GitAPIException {
        git.close();

    }

    private static CredentialsProvider cre(String username, String pw) {
        return new UsernamePasswordCredentialsProvider(username, pw);
    }


    private static Map<String, CredentialsProvider> pwds = new HashMap<>();


    public static void addCre(String name, String pwd) {
        pwds.put(name, cre(name, pwd));
    }

    public static CredentialsProvider cre(String name) {
        return pwds.get(name);
    }


    public static String xingdu = "xingdu";

}
